package com.talkweb.osmharmony.util;

import com.talkweb.osmharmony.ResourceTable;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.*;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.io.IOException;

public final class ResourceHelper {

    private ResourceHelper() {

    }

    public static String[] getStringArray(Context context, int resourceId) {
        String[] resourceStringArray = new String[]{};
        Element element = null;
        try {
            ResourceManager resourceManager = context.getResourceManager();
            element = resourceManager.getElement(resourceId);
            resourceStringArray = element.getStringArray();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return resourceStringArray;
    }

    public static String getString(Context context, int resourceId) {
        String resourceString = "";
        Element element = null;
        try {
            ResourceManager resourceManager = context.getResourceManager();
            element = resourceManager.getElement(resourceId);
            resourceString = element.getString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return resourceString;
    }

    public static Resource getResource(Context context, int resourceId) {
        Resource resource = null;
        try {
            ResourceManager resourceManager = context.getResourceManager();
            resource = resourceManager.getResource(resourceId);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        return resource;
    }

    public static ohos.agp.components.element.Element getElement(Context context, int resourceId) {
        return new PixelMapElement(getResource(context,resourceId));
    }

    /**
     * 根据media id 获取Pixmap
     * @param context
     * @param resourceId
     * @return
     */
    public static PixelMap getPixmap(Context context,final int resourceId) {
        Resource resource = ResourceHelper.getResource(context, resourceId);

        ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
        ImageSource imageSource = ImageSource.create(resource, sourceOptions);

        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
        decodingOptions.editable = false;
        decodingOptions.sampleSize = ImageSource.DecodingOptions.DEFAULT_SAMPLE_SIZE;
        return imageSource.createPixelmap(decodingOptions);
    }

}
